package com.wmlive.hhvideo.heihei.personal.activity;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;

import com.wmlive.hhvideo.common.base.DcBaseActivity;
import com.wmlive.hhvideo.common.navigator.Navigator;
import com.wmlive.hhvideo.heihei.beans.login.UserInfo;
import com.wmlive.hhvideo.heihei.beans.personal.ListFollowerFansEntry;
import com.wmlive.hhvideo.heihei.beans.personal.ListFollowerFansResponse;
import com.wmlive.hhvideo.heihei.beans.personal.UserHomeRelation;
import com.wmlive.hhvideo.heihei.login.AccountUtil;
import com.wmlive.hhvideo.heihei.personal.adapter.FocusAdapter;
import com.wmlive.hhvideo.heihei.personal.presenter.FocusPresenter;
import com.wmlive.hhvideo.heihei.personal.view.IFocusView;
import com.wmlive.hhvideo.utils.ToastUtil;
import com.wmlive.hhvideo.widget.dialog.CustomDialog;
import com.wmlive.hhvideo.widget.refreshrecycler.OnRecyclerItemClickListener;
import com.wmlive.hhvideo.widget.refreshrecycler.RefreshRecyclerView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import cn.wmlive.hhvideo.R;

/**
 * Created by XueFei on 2017/5/31.
 * <p>
 * 关注页
 */

public class FocusActivity extends DcBaseActivity implements SwipeRefreshLayout.OnRefreshListener, RefreshRecyclerView.OnLoadMoreListener, IFocusView, FocusAdapter.OnClickFocusCustom, OnRecyclerItemClickListener {
    @BindView(R.id.rv_focus_list)
    RefreshRecyclerView rvFocusList;

    private FocusAdapter focusAdapter;

    private FocusPresenter focusPresenter;

    private Intent intent;

    private int offset = 0;
    private long user_id = 0;
    private long current_id = 0;
    private UserInfo userInfo;
    private boolean isLoadMore = false;

    // TODO: 2017/6/2 取消粉丝 粉丝 他人粉丝

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_focus;
    }


    @Override
    protected void initData() {
        super.initData();
        focusPresenter = new FocusPresenter(this);
        addPresenter(focusPresenter);

        intent = getIntent();
        userInfo = (UserInfo) intent.getSerializableExtra(KEY_PARAM);
        if (null != userInfo) {
            user_id = userInfo.getId();
            current_id = AccountUtil.getUserId();

            LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
            rvFocusList.setLayoutManager(linearLayoutManager);
            focusAdapter = new FocusAdapter(rvFocusList, new ArrayList<ListFollowerFansEntry>(), current_id);
            rvFocusList.setAdapter(focusAdapter);
            rvFocusList.setOnRefreshListener(this);
            rvFocusList.autoRefresh();
            focusAdapter.setOnRecyclerItemClickListener(this);

            focusAdapter.setOnClickCustom(this);

            if (user_id == current_id) {
                setTitle("我关注的", true);

                focusAdapter.setEmptyStr(R.string.user_home_focus);
            } else {
                setTitle(userInfo.getName() + "的关注", true);

                focusAdapter.setEmptyStr(R.string.user_other_home_focus);
            }
            focusAdapter.setShowImg(true);
        }
    }

    @Override
    public void handleFocusSucceed(ListFollowerFansResponse response) {
        List<ListFollowerFansEntry> users = response.getUsers();

        focusAdapter.addData(!isLoadMore, users);

        offset = response.getOffset();
    }

    @Override
    public void handleFocusFailure(String error_msg) {
        ToastUtil.showToast(error_msg);
        focusAdapter.showError(!isLoadMore);
    }

    @Override
    public void onRefresh() {
        isLoadMore = false;
        offset = 0;
        focusPresenter.getFocusList(user_id, offset);
    }

    @Override
    public void onLoadMore() {
        isLoadMore = true;
        focusPresenter.getFocusList(user_id, offset);
    }

    @Override
    public void handleFocusClickSucceed(boolean isFollower, int resStrId) {
        ListFollowerFansEntry entry = focusAdapter.getItemData(mPosition);
        if (null != entry) {
            UserHomeRelation relation = entry.getRelation();
            if (null != relation) {
                relation.setIs_follow(isFollower);
                focusAdapter.notifyItemChanged(mPosition);
                ToastUtil.showToast(resStrId);
            }
        } else {
            ToastUtil.showToast(R.string.net_error);
        }
    }

    @Override
    public void handleFocusClickFailure(String error_msg) {
        ToastUtil.showToast(error_msg);
    }

    @Override
    protected void onSingleClick(View v) {
    }

    int mPosition = -1;
    private CustomDialog customDialog;

    @Override
    public void onClick(View view, final int position) {
        if (view.getId() == R.id.iv_focus_item_state) {
            if (!AccountUtil.isLogin()) {
                showReLogin();
            } else {
                final ListFollowerFansEntry entry = focusAdapter.getItemData(position);
                if (null != entry) {
                    UserHomeRelation relation = entry.getRelation();
                    if (null != relation) {
                        mPosition = position;
                        // TODO: 2017/6/1 弹出对话框
                        final boolean isFollow = relation.is_follow();
                        if (isFollow) {
                            customDialog = new CustomDialog(this, R.style.BaseDialogTheme);
                            customDialog.setTitle(getString(R.string.dialog_focus_tip));
                            customDialog.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    customDialog.dismiss();
                                    focusPresenter.updateFocusState(isFollow, entry.getId());
                                }
                            });
                            customDialog.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which) {
                                    customDialog.dismiss();
                                }
                            });
                            customDialog.show();
                        } else {
                            focusPresenter.updateFocusState(isFollow, entry.getId());
                        }
                    }
                }
            }
        }
    }

    public static final String KEY_PARAM = "user";

    public static Intent getCallingIntent(Context context, UserInfo userInfo) {
        Intent intent = new Intent(context, FocusActivity.class);
        intent.putExtra(KEY_PARAM, userInfo);
        return intent;
    }

    @Override
    public void onRecyclerItemClick(int dataPosition, View view, Object data) {
        ListFollowerFansEntry entry = (ListFollowerFansEntry) data;
        Navigator.getInstance().navigateToOtherUserHome(this, false, entry.getId());
    }
}
